package com.cindata

import com.cindata.dictionary.*

class Account
{
    Account parent

    String name
    // 企业名称
    String fullName
    // 法人
    String sex
    // 法人性别
    String cellphone
    // 法人电话
    String legalPersonEmail
    // 法人电子邮箱
    Integer workingYears
    // 法人从事房地产经纪年限
    String degree
    // 法人学历
    Province province
    // 所在省
    City city
    // 所在市
    District district
    // 所在区
    String address
    // 地址
    String contact
    // 联系人
    Date establishDate
    // 成立日期
    OrganizationNatureDict unitNature
    // 单位性质
    String scale
    // 企业规模
    String hierarchy
    // 公司层级
    String phone
    // 电话号码（座机号）
    String zipCode
    // 邮编
    String fax
    // 传真
    String email
    // 电子邮箱
    String website
    // 网址
    Integer operatingPeriod
    // 经营期限
    String scopeOfBusiness
    // 经营范围
    String operatingState
    // 经营状态
    String operatingConditions
    // 经营状况
    String description
    // 公司简介
    String creditRating
    // 信用评级
    String qualificationGrade
    // 资质等级
    Date dateOfQualificationRating
    // 资质等级评定日期
    BigDecimal registeredCapital
    // 注册资金
    CurrencyDict currency
    // 注册资金币种
    Boolean isGroupCompany
    // 是否集团公司
    Boolean isGroupHeadquarters
    // 是否集团总部
    Boolean isListedCompany
    // 是否上市公司
    String certificateNumber
    // 证书编号
    String organizationCode
    // 组织机构代码
    String businessLicense
    // 营业执照
    Date startDate
    // 营业期限起始日期
    Date endDate
    // 营业期限到期日期
    Date logoutDate
    // 注销日期
    Date revocationDate
    // 吊销日期
    String postalAddress
    // 通讯地址
    String registerOrg
    // 登记机关
    Date registerDate
    // 工商登记日
    String recordCertificateNumber
    // 备案证书号
    Date recordDate
    // 备案日期
    Date licenseIssuingDate
    // 发照日期
    String unifiedSocialCreditCode
    // 统一社会信用代码
    BigDecimal actuallyRaisingFund
    // 实收资本金
    CurrencyDict raisingFundCurrency
    // 实收资本金币种
    String negativeInfo
    // 负面信息
    BigDecimal subscribedCapital
    // 认缴出资额
    Date investmentDate
    // 出资时间
    String licenseNumber
    // 许可证号
    Date auditDate
    // 审核时间
    String financingInfo
    // 融资信息
    String taxInfo
    // 税务信息
    String clearInfo
    // 清算信息
    String registerType
    // 工商登记注册类型
    String manager
    // 企业总经理
    String recentAnnualInspection
    // 最近年检情况
    String agencyNumber
    // 代理机构编号
    String codeOfRegisterArea
    // 注册地址行政区划代码
    String aptitudeName
    // 资质名称
    Integer duration
    // 有效期限
    Integer shareholdersAmount
    // 股东人数
    Date qualificationIssuingDate
    // 资质发证日期
    Date qualificationEndDate
    // 资质有效期限终止
    String abbreviation
    // 企业简称
    String registeredAddress
    // 注册地址
    AccountTypeDict type
    // 企业类型
    String publicFundSituation
    // 公积金缴存情况
    Integer status = 1
    // 企业状态 0: 禁用 1：启用
    String registrationNumber
    // 注册号
    Date approvalDate
    // 核准日期

    Date createdDate = new Date()
    Date modifiedDate = new Date()
    User user
    User createBy
    User modifyBy

    Double x
    // 经度
    Double y
    // 纬度
    AccountLevelDict level
    // 层级
    String logo
    // 图标

    String creditLevel
    // 信用等级
    BigDecimal creditAmount
    // 信用额度
    BigDecimal creditBalance
    // 信用余额

    Account bankAccount
    // 贷款企业所属的银行机构
    Account agencyAccount
    // 委托机构

    // AccountStage stage
    // 机构审批阶段
    // Workflow workflow
    // 机构初始化工作流

    String retreatResult
    // 机构清退原因
    Date retreatDate
    // 机构清退时间
    Workflow retreatWorkflow
    // 机构清退工作流
    Boolean isInWorkflow = false
    // String workflowStatus = "Pending"
    
    static hasMany = [citys: AccountCity]

    static constraints = {

        parent nullable: true, blank: true

        name maxSize: 50
        fullName maxSize: 20, nullable: true, blank: true
        sex maxSize: 2, nullable: true, blank: true
        cellphone maxSize: 11, nullable: true, blank: true
        legalPersonEmail maxSize: 50, nullable: true, blank: true
        workingYears nullable: true, blank: true
        degree maxSize: 10, nullable: true, blank: true
        degree inList: ["小学", "初中", "高中", "大专", "本科", "硕士研究生", "博士研究生", "其他"]
        province nullable: true, blank: true
        city nullable: true, blank: true
        district nullable: true, blank: true
        address maxSize: 150, nullable: true, blank: true
        contact maxSize: 20, nullable: true, blank: true
        establishDate nullable: true, blank: true
        unitNature nullable: true, blank: true
        scale maxSize: 50, nullable: true, blank: true
        hierarchy maxSize: 50, nullable: true, blank: true
        phone maxSize: 20, nullable: true, blank: true
        zipCode maxSize: 10, nullable: true, blank: true
        fax maxSize: 30, nullable: true, blank: true
        email maxSize: 50, nullable: true, blank: true
        website maxSize: 50, nullable: true, blank: true
        operatingPeriod nullable: true, blank: true
        scopeOfBusiness maxSize: 200, nullable: true, blank: true
        operatingState maxSize: 20, nullable: true, blank: true
        operatingState inList: ['存续', '歇业', '清算', '在业', '吊销', '注销', '迁入', '迁出', '停业']
        operatingConditions maxSize: 200, nullable: true, blank: true
        description maxSize: 200, nullable: true, blank: true
        creditRating maxSize: 20, nullable: true, blank: true
        qualificationGrade maxSize: 20, nullable: true, blank: true
        dateOfQualificationRating nullable: true, blank: true
        registeredCapital nullable: true, blank: true
        currency nullable: true, blank: true
        isGroupCompany nullable: true, blank: true
        isGroupHeadquarters nullable: true, blank: true
        isListedCompany nullable: true, blank: true
        certificateNumber maxSize: 50, nullable: true, blank: true
        organizationCode maxSize: 50, nullable: true, blank: true
        businessLicense maxSize: 30, nullable: true, blank: true
        startDate nullable: true, blank: true
        endDate nullable: true, blank: true
        logoutDate nullable: true, blank: true
        revocationDate nullable: true, blank: true
        postalAddress maxSize: 150, nullable: true, blank: true
        registerOrg maxSize: 50, nullable: true, blank: true
        registerDate nullable: true, blank: true
        recordCertificateNumber maxSize: 50, nullable: true, blank: true
        recordDate nullable: true, blank: true
        licenseIssuingDate nullable: true, blank: true
        unifiedSocialCreditCode maxSize: 50, nullable: true, blank: true
        actuallyRaisingFund nullable: true, blank: true
        raisingFundCurrency nullable: true, blank: true
        negativeInfo maxSize: 200, nullable: true, blank: true
        subscribedCapital nullable: true, blank: true
        investmentDate nullable: true, blank: true
        licenseNumber maxSize: 50, nullable: true, blank: true
        auditDate nullable: true, blank: true
        financingInfo maxSize: 200, nullable: true, blank: true
        taxInfo maxSize: 200, nullable: true, blank: true
        clearInfo maxSize: 200, nullable: true, blank: true
        registerType maxSize: 50, nullable: true, blank: true
        manager maxSize: 20, nullable: true, blank: true
        recentAnnualInspection maxSize: 200, nullable: true, blank: true
        agencyNumber maxSize: 50, nullable: true, blank: true
        codeOfRegisterArea maxSize: 50, nullable: true, blank: true
        aptitudeName nullable: true, blank: true
        duration nullable: true, blank: true
        shareholdersAmount nullable: true, blank: true
        qualificationIssuingDate nullable: true, blank: true
        qualificationEndDate nullable: true, blank: true
        abbreviation maxSize: 50, nullable: true, blank: true
        registeredAddress maxSize: 150, nullable: true, blank: true
        type nullable: true, blank: true
        publicFundSituation maxSize: 200, nullable: true, blank: true
        status maxSize: 10, nullable: true, blank: true
        registrationNumber maxSize: 50, nullable: true, blank: true
        approvalDate nullable: true, blank: true

        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
        user nullable: true, blank: true
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true

        x nullable: true, blank: true
        y nullable: true, blank: true
        level nullable: true, blank: true
        logo maxSize: 50, nullable: true, blank: true

        creditLevel maxSize: 50, nullable: true, blank: true
        creditAmount nullable: true, blank: true
        creditBalance nullable: true, blank: true

        bankAccount nullable: true, blank: true
        agencyAccount nullable: true, blank: true

        // stage nullable: true, blank: true
        // workflow nullable: true, blank: true

        retreatResult maxSize: 200, nullable: true, blank: true
        retreatDate nullable: true, blank: true
        retreatWorkflow nullable: true, blank: true

        isInWorkflow nullable: true, blank: true

        // workflowStatus maxSize: 20, nullable: true, blank: true
        // workflowStatus inList: ["Pending", "Failed", "Completed"]
    }

    static mapping = {
        sort 'name'
        autowire true
    }

    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        def user = User.findByUsername(username)
        createBy = user

        bankAccount = user?.account
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }

    static FilterAccountSql(User user)
    {
        def accountList = AccountManageAccount.where {
            account == user?.account
        }.manageAccount.list()

        String accountSql = "(" + user?.account?.id + ")"
        if (accountList)
        {
            String accountIdList = accountList*.id?.toString()
            accountSql = "(" + accountIdList?.substring(1, accountIdList.lastIndexOf("]")) + ")"

        }
        return accountSql
    }

    def toHM()
    {
        def hashObj = [id: this.id,
            name: this?.name,
            description: this?.description,
            parentId: this?.parent?.id,
            phone: this?.phone,
            cellphone: this?.cellphone,
            address: this?.address,
            contact: this?.contact,
            province: this?.province,
            city: this?.city,
            district: this?.district,
            x: this?.x,
            y: this?.y,
            level: this?.level,
            createdDate: this?.createdDate,
            modifiedDate: this?.modifiedDate,
            children: []]
        return hashObj
    }

    static ToHashMapArr(accountArr)
    {
        def hashArr = []
        accountArr.each {
            hashArr.push(it.toHM())
        }
        return hashArr
    }

    static GetTreeDataByArr(accountArr)
    {
        def rootArr = GetRootArr(accountArr)
        rootArr.each {
            GetChildrenAccountArr(it, accountArr)
        }
        return rootArr
    }

    static GetRootArr(accountArr)
    {
        def rootArr = []
        accountArr.each {
            if (it.parentId == null)
            {
                rootArr.push(it)
            }
            else
            {
                def tmpArr = accountArr.findAll { cit -> cit.id == it.parentId }
                if (tmpArr.size == 0)
                {
                    rootArr.push(it)
                }
            }
        }
        return rootArr
    }

    static GetChildrenAccountArr(account, accountArr)
    {
        def tmpArr = accountArr.findAll { it.parentId == account.id }
        if (tmpArr.size != 0)
        {
            account.children = tmpArr
        }
        account.children.each { cit -> GetChildrenAccountArr(cit, accountArr)
        }
    }

    String toString()
    {
        name
    }
}
